<?php

class Application_Model_Users
{


}


class Db_Users extends Zend_Db_Table 
{ 

    protected $_name = 'users';
    
    protected $_sequence = true;

//Проверки***********************************************
    public function check($where)
    {
	return $this->fetchAll($where)->count() > 0 ? true : false;
    }
    
    public function checkId($id)
    {
	return $this->check('id="'.$id.'"');
    }
    
    public function checkLogin($login)
    {
	return $this->check('login="'.$login.'"');
    }
    
    public function checkEmail($email)
    {
	return $this->check('email="'.$email.'"');
    }


//Добаление/Удаление****************************************    
    public function addUser($login,$password,$email)
    {
	if (($this->checkLogin($login)) or ($this->checkEmail($email)))
	    return false;
	else
	{
	    $data = array
	    (
		'login' 	=> $login,
		'password'	=> md5($password),
		'email'	=> $email
	    );
	    return $this->insert($data);
	};
    
    }
    
    public function delUser($id)
    {
	return ($this->checkId($id)) ? $this->delete('id="'.$id.'"') : false ;
    }
    
//Чтение/Изменение параметров пользователя    
    public function getEmail($id)
    {
	return ( $data=$this->fetchRow('id="'.$id.'"') ) ? $data['email'] : false ;
    }
    
    public function setEmail($id,$value)
    {
	$setdata = array ( 'email' =>  $value );
	return ( $this->update($setdata,'id="'.$id.'"') > 0 ) ? true : false ;
    }

    public function getIdByLogin($login)
    {
	return ( $data=$this->fetchRow('login="'.$login.'"') ) ? $data['id'] : false ;
    }
}